import Vue from 'vue'
import VueRouter from 'vue-router'


// 解决重复导航点击
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 我的 二级路由
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index', component: () => import('@/views/index.vue'),
      children: [
        { path: '/indexSearch', component: () => import('@/pages/public/Search/Serach.vue') },
        { path: '/indexGoodsInfo', component: () => import('@/pages/public/GoodsInfo/goodsInfo.vue') }
      ]
    },
    {
      path: '/sort', component: () => import('@/views/sort.vue'),
      children: [
        { path: '/sortSearch', component: () => import('@/pages/public/Search/Serach.vue') },
      ]
    },

    {
      path: '/cart', component: () => import('@/views/cart.vue'),
      children: [
        { path: '/cartGoodsInfo', component: () => import('@/pages/public/GoodsInfo/goodsInfo.vue') },
        {
          path: '/orderIndex',
          component: () => import('@/pages/cart/order/index.vue'),
          children: [
            {
              path: '/orderAddress',
              component: () => import('@/pages/public/_Address/Address.vue'),
              children: [
                { path: '/OrderEditAddress', component: () => import('@/pages/public/_Address/EditAddress') }
              ]
            }
          ]
        }
      ]
    },
    {
      path: '/my', component: () => import('@/views/my.vue'),
      children: [
        { path: '/login', component: () => import('@/pages/my/Login/Login.vue') },
        {
          path: '/address', component: () => import('@/pages/public/_Address/Address.vue'),
          children: [
            { path: '/editAddress', component: () => import('@/pages/public/_Address/EditAddress.vue') },
          ]
        },
        { path: '/serve', component: () => import('@/pages/my/ListView/Serve.vue') },
        { path: '/baseCenter', component: () => import('@/pages/my/ListView/BaseCenter.vue') },
        { path: '/setting', component: () => import('@/pages/my/ListView/Setting.vue') },
        { path: '/order', component: () => import('@/pages/my/ListView/Order/Order.vue') }
      ]
    },
  ]
})
import pathArr from '@/router/pathArr.js'
// 导航守卫
router.beforeEach((to, from, next) => {
  if (pathArr.indexOf(to.path) != -1) {
    // 拿到token值
    const token = JSON.parse(window.localStorage.getItem('userInfo'));
    if (token) {
      next();
    } else {
      // 不允许通过
      next('/Login');
    }
  } else {
    next();
  }
})

export default router